
package ogame.utilities.buildings;

import java.util.HashMap;

/**
 *
 * @author scchang
 */
public class Buildings extends HashMap<BuildingType, Integer>{
    
    public Buildings(){
        super();
        for(BuildingType type : BuildingType.values()){
            // Initialize all to 0
            this.put(type, 0);
        }
    }
    
    public String toString(){
        String output = "";
        for(BuildingType building : this.keySet()){
            output += building.name() + " : " + this.get(building) + '\n';
        }
        if(output.equals("")){ output += '\n'; }
        return output;
    }
    
    public static Buildings parseBuildings(byte[] input){
        Buildings result = new Buildings();
        for(int i = 0 ; i < input.length; ++i){
            result.put(BuildingType.values()[i], (int)input[i]);
        }
        return result;
    }
    
    public byte[] encode(){
        byte[] output = new byte[BuildingType.values().length];
        for(int i = 0; i < BuildingType.values().length; ++i){
            output[i] = Byte.parseByte("" + this.get(BuildingType.values()[i]));
        }
        return output;
    }
    
    public static boolean unitTest(){
        Buildings buildings = new Buildings();
        
        int i = 0;
        for(BuildingType type: BuildingType.values()){
            buildings.put(type, i);
            i++;
        }
        
        Buildings result = Buildings.parseBuildings(buildings.encode());
        i = 0;
        for(BuildingType type: BuildingType.values()){
            if(result.get(type) != i){
                System.out.println("With type: " + type + " got: " + result.get(type)
                        + ". Expected: "+i);
                return false;
            }
            i++;
        }
        
        result = Buildings.parseBuildings((new Buildings()).encode());
        for(BuildingType type: BuildingType.values()){
            if(result.get(type) != 0){
                System.out.println("With type: " + type + " got: " + result.get(type)
                        + ".  Expected: 0");
                return false;
            }
        }
        
        
        return true;
    }
}
